<?php
	/**
	 * Home controller class
	 * @version 1.0
	 * @package controllers
	*/
	Class HomeController extends Controller 
	{
		public $page_title = LANG_WELCOME;
		public $layout = 'site';
		public $name = 'home';
		public $module = 'home';
		public $localName = LANG_WELCOME;
		public $metaName = META_NAME_HOME_PAGE;
		public $metaDescription = META_DESCRIPTION_HOME_PAGE;
		public $helpers = array('html', 'htmls', 'htmlx');
		public $javascript = array();
		public $publicPages = array('index', 'support');
		public $components = array('gmail|support');

		
		/* PUBLIC PAGES */
		
		/**
		 * Home page
		 */
		function Index()
		{
			if (!empty(HTTPContext::$session['client'])) 
			{
				$this->Redirect('clients/account/');
			}
			
			$this->page_title = Language::Get('Home');
			$this->menuItem = 'home';
			$this->layout = 'home';
			
			//reference data
			$this->data['account_types'] = $this->GetListFromPool('AccountTypes');
			
			//on logging in
			if (HTTPContext::IsPostBack())
			{
				//validation
				if (!filter_var(HTTPContext::$post['email'], FILTER_VALIDATE_EMAIL)) $this->controller_model->validation_errors['email'] = Language::Get('Should be valid');
				if (empty(HTTPContext::$post['email'])) $this->controller_model->validation_errors['email'] = Language::Get('is required');
				if (empty(HTTPContext::$post['password'])) $this->controller_model->validation_errors['password'] = Language::Get('is required');

				//if not validated
				if (!empty($this->controller_model->validation_errors))
				{
					$this->data['message']['text'] = Language::Get('Login or password are incorrect');
					$this->data['message']['cssClass'] = 'error';
				}
				//if validated
				else 
				{
					$password = md5(HTTPContext::$post['password']);
					$client = $this->GetModelObject('Clients')->Execute(array('email' => HTTPContext::$post['email'], 'password' => $password), 'sp_clients_auth');
					
					//on success
					if (!empty($client))
					{
						$_SESSION['client'] = $client;
						
						//lotteries draw
						$this->DrawLotteries();
//						if (!empty($_SESSION['redirect_url']))
//						{
//							header("Location: ".$_SESSION['redirect_url']);
//							unset($_SESSION['redirect_url']);
//							exit();
//						}
//						else 
//						{
							$language = (!empty($client['language_id'])) ? $this->GetModelObject('Languages')->Get($client['language_id']) : array('name' => '');
							$this->Redirect('clients/account/?lang='.$language['name']);
//						}
					}
					//on fail
					else 
					{
						$this->controller_model->validation_errors['password'] = Language::Get('incorrect');
						$this->data['message']['text'] = Language::Get('Login failed');
						$this->data['message']['cssClass'] = 'error';
					}
				}
			}
			else
			{
				$this->request->post['email'] = $_COOKIE['email'];
				$this->request->post['password'] = $_COOKIE['password'];
			}	
		}
	
		
		/* AJAX METHODS */
		
		/**
		 * Sends support message
		 *
		 */
		function Support()
		{
			if (HTTPContext::IsPostBack())
			{
				if (Gmail::Send(array
				(
					'subject' => HTTPContext::$post['support_question'],
					'text' => HTTPContext::$post['support_details'],
					'from' => HTTPContext::$post['support_email'],
					'fromName' => HTTPContext::$post['support_name'],
					'recipients' => array('markeddhelp@gmail.com')
				)) === true)
				{
					echo ": ".Language::Get('Support sent');
				}
				else 
				{
					echo ": ".Language::Get('Support error');
				}
				exit();
			}
			
			return;
		}
	}
?>